
//
//  LJHMoreListViewController.m
//  HEARME
//
//  Created by mac on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHMoreListViewController.h"
#import "LJHAlbumListTableViewCell.h"
#import "LJHTextTableCell.h"
#import "UIImageView+WebCache.h"
#import "LJHMusicListViewController.h"
#import "LJHAlbumsDetailViewController.h"
@interface LJHMoreListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic,retain)LJHBaseTableView *myTableView;
@property(nonatomic,retain)NSMutableArray *myArray;
@property(nonatomic,assign)NSInteger currentPage;

@end

@implementation LJHMoreListViewController
-(void)dealloc
{
    [_myArray release];
    [_myTableView release];
    [_myTitle release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [LJHPlayBtn shareButton].frame =CGRectMake(WIDTH*0.4, HEIGHT-WIDTH*0.2-64, WIDTH*0.2, HEIGHT*0.2);
    [self.view addSubview :[LJHPlayBtn shareButton]];
    [[LJHPlayBtn shareButton].musicBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [super viewWillAppear:animated];
    if ([LJHPlayerViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        [[LJHPlayBtn shareButton] startCircle];
        
    }
    if ([LJHPlayerViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [[LJHPlayBtn shareButton] stopCircle];
        
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [GiFHUD setGifWithImageName:@"loading.gif"];
    [GiFHUD show];
    
    //初始化数组
    self.myArray = [NSMutableArray array];
    
    [self createTableView];
    
    //返回按钮
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.title = self.myTitle;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.currentPage = 1;
}

#pragma mark - playButton

-(void)buttonAction:(UIButton *)btn
{
    
    LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    

}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    
    [self.navigationController popViewControllerAnimated:YES];
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"createBigButton" object:[TOPlayButton shareButton]];
    
}

#pragma mark - 创建tableView
-(void)createTableView
{
    self.myTableView = [[LJHBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*0.9) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.myTableView registerClass:[LJHTextTableCell class] forCellReuseIdentifier:@"moreListTableCell"];
    [self.myTableView registerClass:[LJHAlbumListTableViewCell class] forCellReuseIdentifier:@"editorListTableCell"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //头部刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upTableView)];
    self.myTableView.header = header;
    
    [self.myTableView.header beginRefreshing];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downTableView)];
    self.myTableView.footer = footer;
    
    
    
}

#pragma mark - 下拉刷新
- (void)upTableView{
    
    
    if ([self.myTitle isEqualToString:@"精品听单"]) {
        self.moreUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?device=iphone&page=%ld&per_page=10&title=更多",self.currentPage];
    }
    else
    {
        self.moreUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iphone&pageId=%ld&pageSize=20&title=更多",self.currentPage];
    }
    
    [self addData: self.moreUrl andDwon:NO];
}

#pragma mark - 上拉刷新
- (void)downTableView{
    
    if ([self.myTitle isEqualToString:@"精品听单"]) {
        self.moreUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?device=iphone&page=%ld&per_page=10&title=更多",self.currentPage+1];
    }
    else
    {
        self.moreUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iphone&pageId=%ld&pageSize=20&title=更多",self.currentPage+1];
    }
    
    [self addData:self.moreUrl andDwon:YES];
}

#pragma mark - 解析数据

- (void)addData:(NSString *)url andDwon:(BOOL)isDown
{
    [LJHAppTools afnGetRequest:url block:^(id result) {
        
        NSMutableArray *dataArray = [result objectForKey:@"list"];
        
        for (NSMutableDictionary *dic in dataArray) {
            if ([self.myTitle isEqualToString:@"精品听单"]) {
                LJHChildListModel *model = [LJHChildListModel modelWithDictionary:dic];
                [self.myArray addObject:model];
            }
            else if ([self.myTitle isEqualToString:@"小编推荐"])
            {
                LJHAlbumModel *model = [LJHAlbumModel modelWithDictionary:dic];
                model.isFavor = NO;
                
                if ([[[LJHFMData shareFMDB] selectAlbumIdData] containsObject:model.myId]) {
                    model.isFavor = YES;
                }
                
                [self.myArray addObject:model];
                
            }
            
            
        }
        
        if (isDown) {
            self.currentPage++;
        }
        
        [self.myTableView.header endRefreshing];
        [self.myTableView.footer endRefreshing];
        [self.myTableView reloadData];
    } error:^(NSError *error) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        
    }];
    
}



#pragma mark - tableView协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.myArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.myTitle isEqualToString:@"精品听单"]) {
        
        LJHTextTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreListTableCell"];
        
        LJHChildListModel *model = [self.myArray objectAtIndex:indexPath.row];
        cell.textModel = model;
        [cell.newsPic sd_setImageWithURL:[NSURL URLWithString:model.coverPathSmall] placeholderImage:PLACEHOLDER];
        return cell;
        
    }
    else
    {
        LJHAlbumListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"editorListTableCell"];
        LJHAlbumModel *model = [self.myArray objectAtIndex:indexPath.row];
        cell.albumModel = model;
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH*0.3;
}

#pragma mark - 点击cell跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.myTitle isEqualToString:@"精品听单"]){
        LJHMusicListViewController *musicListVC = [[LJHMusicListViewController alloc] init];
        musicListVC.specialId = ((LJHChildListModel *)self.myArray[indexPath.row]).specialId;
        [(LJHTabBarViewController *)self.tabBarController isHiddenTabBar:YES withViewController:musicListVC];
        [self.navigationController pushViewController:musicListVC animated:YES];
    }
    else if ([self.myTitle isEqualToString:@"小编推荐"])
    {
        
        LJHAlbumsDetailViewController *albumsDetailVC = [[LJHAlbumsDetailViewController alloc] init];
        albumsDetailVC.albumId = ((LJHAlbumModel *)self.myArray[indexPath.row]).myId;
        albumsDetailVC.myTitle = _myTitle;
        [(LJHTabBarViewController *)albumsDetailVC.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
